package de.haw.shc.Config;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import de.haw.shc.Device.DeviceItemFragment;
import de.haw.shc.R;
import de.haw.shc.Room.RoomItemFragment;
import de.haw.shc.Szenario.ECO_Szenario;
import de.haw.shc.Szenario.LoveSzenario;
import de.haw.shc.Szenario.TimerSzenario;
import de.haw.shc.Szenario.WakeUpSzenario;

import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: mizus
 * Date: 11.06.13
 * Time: 12:11
 * To change this template use File | Settings | File Templates.
 */
public class TabsAdapter extends FragmentPagerAdapter implements ActionBar.TabListener, ViewPager.OnPageChangeListener {

    private final Context mContext;
    private final ActionBar mActionBar;
    private final ViewPager mViewPager;
    private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();


    private int iD;

    static final class TabInfo {
        private final Class<?> clss;
        private final Bundle bundle;

        TabInfo(Class<?> _class, Bundle _args) {
            clss = _class;
            bundle = _args;
        }
    }

    public TabsAdapter(FragmentActivity activity, ViewPager pager){
        super(activity.getSupportFragmentManager());
        mViewPager = pager;
        mViewPager.setAdapter(this);
        mViewPager.setOnPageChangeListener(this);
        this.mContext = activity;
        this.mActionBar = activity.getActionBar();
    }

    public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args, int icon, int ID) {
        TabInfo info = new TabInfo(clss, args);


        tab.setIcon(icon);
        tab.setTag(info);
        tab.setTabListener(this);
        mTabs.add(info);
        mActionBar.addTab(tab);
        notifyDataSetChanged();
        iD = ID;

    }


    public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args, int ID) {
        iD = ID;
        TabInfo info = new TabInfo(clss, args);
        tab.setTag(info);
        tab.setTabListener(this);
        mTabs.add(info);
        mActionBar.addTab(tab);
        notifyDataSetChanged();
    }

    @Override
    public Fragment getItem(int i) {
       TabInfo info = mTabs.get(i);
       return Fragment.instantiate(mContext, info.clss.getName(), info.bundle);
    }

    private void out(String str){
        System.err.println(str);
    }

    @Override
    public int getCount() {
       return mTabs.size();
    }


    @Override
    public void onPageSelected(int i) {
        mActionBar.setSelectedNavigationItem(i);
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
        Object tag = tab.getTag();
        for (int i = 0; i < mTabs.size(); i++) {
            if (mTabs.get(i) == tag) {
                mViewPager.setCurrentItem(i);
            }
        }
    }

    @Override
    public void onPageScrolled(int i, float v, int i2) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void onPageScrollStateChanged(int i) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
        //To change body of implemented methods use File | Settings | File Templates.
    }
}
